package com.gem.day15.生产者消费者模式;

/**吃货线程类 -- 消费者*/
public class ChiHuo extends Thread {

	//共享资源
	private BaoZi baoZi;

	public ChiHuo(String name, BaoZi baoZi) {
		super(name);
		this.baoZi = baoZi;
	}

	@Override
	public void run() {
		while (true) {
			//同步
			synchronized (baoZi) {
				//没有包子
				if (baoZi.flag == false) {
					//吃货没得吃,只能等待
					try {
						baoZi.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				System.out.println("吃货正在吃" + baoZi.pier + baoZi.xianer + "包子.");
				//包子被吃掉了
				baoZi.flag = false;
				//唤醒
				baoZi.notify();
			}
		}
	}
}
